package com.example.vieweventdemo;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.widget.Scroller;
import android.widget.TextView;

/**
 * Created by tangjiangxu on 9/18/17.
 */

public class TestView extends TextView implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
    private static final String TAG = "TestView";
    private VelocityTracker velocityTracker;
    private GestureDetector gestureDetector;
    private boolean needGesture = false;

    private Scroller scroller = new Scroller(getContext());

    public TestView(Context context) {
        this(context, null);
    }

    public TestView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    /**
     * 初始化操作
     */
    private void init() {
        velocityTracker = VelocityTracker.obtain();
        gestureDetector = new GestureDetector(this);
        //解决长按屏幕后无法拖动的问题
        gestureDetector.setIsLongpressEnabled(false);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (needGesture) {
            boolean consume = gestureDetector.onTouchEvent(event);
            return consume;
        }

       /* velocityTracker.addMovement(event);
        getVelocity();*/
        return super.onTouchEvent(event);
    }

    private void getVelocity() {
        velocityTracker.computeCurrentVelocity(10000);
        int x = (int) velocityTracker.getXVelocity();
        int y = (int) velocityTracker.getYVelocity();
        Log.e(TAG, "getVelocity: x:" + x + ",y:" + y);
    }

    private void myRecycle() {
        velocityTracker.clear();
        velocityTracker.recycle();
    }

    @Override
    public boolean onDown(MotionEvent e) {
        Log.e(TAG, "onDown: ");
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        Log.e(TAG, "onShowPress: ");
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        Log.e(TAG, "onSingleTapUp: ");
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.e(TAG, "onScroll: ");
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        Log.e(TAG, "onLongPress: ");
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.e(TAG, "onFling: ");
        return false;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        Log.e(TAG, "onSingleTapConfirmed: ");
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Log.e(TAG, "onDoubleTap: ");
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        Log.e(TAG, "onDoubleTapEvent: ");
        return false;
    }

    public void smoothScrollTo(int destX, int destY) {
        int scrollX = getScrollX();
        int delta = destX - scrollX;
        //1000ms内滑向destX,效果就是慢慢滑动
        scroller.startScroll(scrollX, 0 ,delta, 0, 10000);
        invalidate();
    }

    @Override
    public void computeScroll() {
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(), scroller.getCurrY());
            postInvalidate();
        }
    }
}
